//
//  SLLottieObject.swift
//  SlockApp
//
//  Created by 凯朱 on 2024/10/9.
//

import Foundation

import UIKit
import Lottie
typealias AnimationCompletionCallback = () -> Void

@objc class AnimationLoader: NSObject {

    @objc static func loadAnimation(from url: String, into view: UIView) -> LottieAnimationView? {
        let oldAnimationView = view.viewWithTag(100989)
        if (oldAnimationView != nil) {
            oldAnimationView?.removeFromSuperview()
        }
        let animationView = LottieAnimationView.init(name: url)
        animationView.tag = 100989;
        view.addSubview(animationView)
        animationView.contentMode = .scaleAspectFill
        animationView.frame = view.bounds
        animationView.loopMode = .playOnce
        return animationView
    }
    
    @objc static func loadAnimationRepeatAndPlay(from url: String, into view: UIView) -> LottieAnimationView? {
        let oldAnimationView = view.viewWithTag(100989)
        if (oldAnimationView != nil) {
            oldAnimationView?.removeFromSuperview()
        }
        let animationView = LottieAnimationView.init(name: url)
        animationView.tag = 100989;
        animationView.contentMode = .scaleAspectFill
        view.addSubview(animationView)
        animationView.frame = view.bounds
        animationView.loopMode = .repeat(999)
        animationView.play()
        return animationView
    }
    
    @objc static func loadAnimationRepeat(from url: String, into view: UIView) -> LottieAnimationView? {
        let oldAnimationView = view.viewWithTag(100989)
        if (oldAnimationView != nil) {
            oldAnimationView?.removeFromSuperview()
        }
        let animationView = LottieAnimationView.init(name: url)
        animationView.tag = 100989;
        animationView.contentMode = .scaleAspectFill
        view.addSubview(animationView)
        animationView.frame = view.bounds
        animationView.loopMode = .repeat(999)
        return animationView
    }
    
    @objc static func playAnimation(animationView: LottieAnimationView, for duration: TimeInterval) {
        if animationView.isAnimationPlaying{
            return
        }
        animationView.play()
        
        // Pause the animation after the specified duration
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
            animationView.pause()
        }
    }
    
    @objc static func playAnimationAndStop(animationView: LottieAnimationView, for duration: TimeInterval) {
        if animationView.isAnimationPlaying{
            return
        }
        animationView.play()
        
        // Pause the animation after the specified duration
        DispatchQueue.main.asyncAfter(deadline: .now() + duration) {
            animationView.stop()
        }
    }
    
    
    @objc static func playAnimationAndStop(animationView: LottieAnimationView, for duration: TimeInterval, completion: AnimationCompletionCallback?) {
        if animationView.isAnimationPlaying {
            return
        }
        animationView.play()
        if let callback = completion {
            callback()
        }
    }
  
    
    @objc static func resetAnimation(animationView: LottieAnimationView) {
        animationView.stop()
    }
}
